﻿using UnityEngine;

namespace GameToolkit
{
    public class PlaybackAction : MonoBehaviour
    {
        PlaybackTrack.PlaybackController mController;

        PlaybackTrack.ESearchMarkType mSearchMark;
        string mSearchName;
        bool mPause;
        bool mWasPause;

        internal void ProcessFrame(PlaybackTrack.PlaybackController ctrl)
        {
            var dirty = mController != ctrl;
            mController = ctrl;
            if (dirty || mPause != mWasPause)
            {
                mWasPause = mPause;
                if (mPause)
                    ctrl.Pause(this);
                else
                    ctrl.Resume(this);
            }
            if (mSearchMark != PlaybackTrack.ESearchMarkType.None)
            {
                ctrl.JumpToMark(mSearchName, mSearchMark);
                mSearchMark = PlaybackTrack.ESearchMarkType.None;
            }
        }

        internal void Release(PlaybackTrack.PlaybackController ctrl)
        {
            if(ctrl == mController)
            {
                mController = null;
                mPause = false;
                mWasPause = false;
                mSearchMark = PlaybackTrack.ESearchMarkType.None;
            }
        }

        [ContextMenu("Pause Timeline")]
        public void PauseTimeline()
        {
            mPause = true;
            if (mController != null)
                ProcessFrame(mController);
        }

        [ContextMenu("Resume Timeline")]
        public void ResumeTimeline()
        {
            mPause = false;
            if (mController != null)
                ProcessFrame(mController);
        }

        public void JumpToNextMark(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.Next;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }


        public void JumpToNextMarkStart(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.NextStart;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }

        public void JumpToNextMarkEnd(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.NextEnd;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }

        public void JumpToPrevMark(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.Previours;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }


        public void JumpToPrevMarkStart(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.PrevStart;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }

        public void JumpToPrevMarkEnd(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.PrevEnd;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }

        public void JumpToEntireMark(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.Entire;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }

        public void JumpToEntireMarkStart(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.EntireStart;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }

        public void JumpToEntireMarkEnd(string mark)
        {
            if (!string.IsNullOrEmpty(mark))
            {
                mSearchMark = PlaybackTrack.ESearchMarkType.EntireEnd;
                mSearchName = mark;
                if (mController != null)
                    ProcessFrame(mController);
            }
        }
    }
}
